package com.situ.blog.dao.impl;


import com.situ.blog.dao.IBlogDao;
import com.situ.blog.pojo.entity.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDaoImpl implements IBlogDao {
    @Override
    public List<Blog> selectByPage(BlogQuery blogQuery) {
        int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Blog> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,title,image,click_count,content,type_id,status,deleted,gmt_create,gmt_modified FROM blog where 1=1 ";
            //这里面放所有搜索条件
            List<Object> queryList = new ArrayList<>();
            String queryName = blogQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and title like ? ";
                queryList.add("%" + queryName + "%");
            }
            sql += " limit ?,?";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            preparedStatement.setInt(queryList.size() + 1, offset);
            preparedStatement.setInt(queryList.size() + 2, blogQuery.getLimit());

            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                int id = resultSet.getInt("id");
                String title = resultSet.getString("title");
                String image = resultSet.getString("image");
                int clickCount = resultSet.getInt("click_count");
                String content = resultSet.getString("content");
                int typeId = resultSet.getInt("type_id");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("deleted");
                java.util.Date gmtCreate = resultSet.getTimestamp("gmt_create");
                java.util.Date gmtModified = resultSet.getTimestamp("gmt_modified");
                Blog blog = new Blog(id, title, image, clickCount, content, typeId, status, deleted, gmtCreate, gmtModified);
                list.add(blog);
            }
            for (Blog blog : list) {
                System.out.println(blog);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }

    @Override
    public Integer selectTotalCount(BlogQuery blogQuery) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from blog where 1=1 ";
            List<Object> queryList = new ArrayList<>();
            String queryName = blogQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and title like ? ";
                queryList.add(queryName);
            }
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                totalCount = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalCount;
    }

    @Override
    public int deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from blog where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public int add(Blog blog) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into blog(title,image,content,type_id) values(?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, blog.getTitle());
            preparedStatement.setString(2, blog.getImage());
            preparedStatement.setString(3, blog.getContent());
            preparedStatement.setInt(4, blog.getTypeId());
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }



    @Override
    public int updateStatus(int id, int status) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update blog set status=? where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, status);
            preparedStatement.setInt(2, id);
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
}
